<template>
	<view>
		<view class="d-flex j-sb a-center">
			<view class="font-lg font-weight">物品管理</view>
			
			<view class="rounded bg-primary button font-md text-white d-flex a-center j-center"  @click="onAdd">新增</view>
		</view>
	
		<uni-table class="mt-1" style="height: 280rpx"  ref="table" border stripe emptyText="暂无更多数据">
			<uni-tr class="position-sticky bg-white" >
				<uni-th><text class="font-md">物品名称</text></uni-th>
				<uni-th width="250"><text class="font-md">物品分类</text></uni-th>
				<uni-th width="70"><text class="font-md">数量</text></uni-th>
				<uni-th width="120"><text class="font-md">存放位置</text></uni-th>
				<uni-th><text class="font-md">备注</text></uni-th>
				<uni-th width="140"><text class="font-md">操作</text></uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td>
					<text class="font-md" v-html="item.name"></text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{getCatrgryName(item.cid)}}</text>
				</uni-td>
				<uni-td>
					<text class="font-md" v-html="item.num"></text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.boxId < 10?'0'+item.boxId:item.boxId}}号箱格</text>
				</uni-td>
				<uni-td>
					<text class="font-md" >{{item.notes}}</text>
				</uni-td>
				<uni-td>
					
					<view class="d-flex a-center">
						<!-- <view @click="onView(item)" class="font-md text-primary">查看</view> -->
						<view @click="onEdit(item)" class="font-md text-primary ml">编辑</view>
						<view @click="onDel(item)" class="font-md text-blue ml">删除</view>
					</view>
					
				</uni-td>
			</uni-tr>
		</uni-table>
		
		<uni-popup ref="popupRef" type="center">
			<view class="p-2 bg-white model-shell ">
				<view class="font-lg font-weight">{{popupTitle}}</view>
				<view class="px-2 mt-2 font-md">
					<uni-forms labelWidth="46rpx" ref="formRef"  :modelValue="formData" :rules="rules">
						
						<uni-forms-item required class="mt-1"  label="物品名称:" name="name">
							<uni-easyinput type="text" v-model="formData.name" placeholder="请输入物品名称" />
						</uni-forms-item>	
						<uni-forms-item required  label="物品分类:" name="pid">
							<uni-data-picker v-model="formData.cid"  popup-title="请选择物品分类" :localdata="dataPickerTree" placeholder="选择物品分类" />
						</uni-forms-item>
						<uni-forms-item required class="mt-1"  label="库存数量:" name="num">
							<uni-easyinput type="number" v-model="formData.num" placeholder="请输入库存数量" />
						</uni-forms-item>
				
						<uni-forms-item  label="存放位置:" name="boxId">
							<view class="d-flex a-center">
								<view class="is-input-border d-flex a-center pl flex-1 mr"  @click="onBoxList">
									{{formData.boxId < 10? '0' + formData.boxId : formData.boxId}} 
								</view>号箱/柜
							</view>
						</uni-forms-item>
						
						<uni-forms-item  label="备注:" name="boxId">
							<view class="d-flex a-center">
								<uni-easyinput type="textarea" v-model="formData.notes" placeholder="请输入输入备注" />
							</view>
						</uni-forms-item>
					</uni-forms>
					
					<view class="d-flex mt-3 ">
						<view class="flex-1 px-1 d-flex a-center j-center">
							<view class="rounded bg-secondary button font-md text-white" @click="popupRef.close()">取消</view>
						</view>
						<view class="flex-1 px-1 d-flex a-center j-center">
							<view class="rounded bg-primary button font-md text-white" type="primary" @click="onsave">保存</view>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
		
		<uni-popup ref="boxListRef" type="center">	
			<view style="width: 90vw;height: 90vh;" class="h-100 bg-white rounded">
				<view class="p-2">
					<view class="font-big">请选择箱格</view>
					<view class="mt-1">
						<view class="d-flex flex-row flex-wrap">
							<scroll-view scroll-y="true" style="height: 320rpx;" >	
								<view @click="onSelected(item)" v-for="(item,index) in boxList" :key="item" class="boxItem d-flex">
									<view :class="[item.goods.length == 0?'bg-secondary':'bg-green']" class="flex-1 rounded-min d-flex a-center j-center font-lg text-white">{{item.code}}</view>
								</view>
							</scroll-view>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
		
		<uni-popup ref="boxinfoRef" type="center">
			<view class="p-2 bg-white model-shell ">
				<view class="font-lg font-weight">{{boxinfo.code}}箱格</view>
				<view class="px-2 mt-2 font-md">
					
					<uni-table class="mt-1" style="height: 140rpx"  ref="table" border stripe emptyText="暂无存放物品">
						<uni-tr class="position-sticky bg-white" >
							<uni-th><text class="font-md">存放物品名称</text></uni-th>
							<uni-th><text class="font-md">数量</text></uni-th>
						</uni-tr>
						<uni-tr v-for="(item, index) in boxinfo.goods" :key="index">
							<uni-td>
								<text class="font-md" v-html="item.name"></text>
							</uni-td>
							<uni-td>
								<text class="font-md" v-html="item.num"></text>			
							</uni-td>
						</uni-tr>
					</uni-table>
					
					<view class="d-flex mt-3 ">
						<view class="flex-1 px-1 d-flex a-center j-center">
							<view class="rounded bg-secondary button font-md text-white" @click="boxinfoRef.close()">取消</view>
						</view>
						<view class="flex-1 px-1 d-flex a-center j-center">
							<view class="rounded bg-primary button font-md text-white" type="primary" @click="onSelected(boxinfo,boxinfo.id)">开柜</view>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
		
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { useStore } from '@/store/index.js';
const store = useStore();

const tableData = ref([])
const dataPickerTree = ref([]);
const popupRef= ref(null);
const popupTitle = ref('');

const formData = ref({});
const formRef = ref(null);
const rules = ref({
	name: {
		rules: [{
			required: true,
			errorMessage: '请输入分类名称',
		}],
	}
});

const boxListRef = ref(null);
const boxList = ref([]);

const boxinfoRef = ref(null);
const boxinfo = ref({});


const getCatrgryName = (id) => {
	return store.categoryStore.getNameStrand(id)
}


const reLoad = () => {
	tableData.value = store.goodsStore.getList();
	boxList.value = store.goodsStore.getBoxList();
	dataPickerTree.value = store.categoryStore.getDataPickerTree()
	formData.value = {id:'',name:'',cid:'',num:1,boxId:''};
};

const onBoxList = () => {
	boxListRef.value.open();
};

const onSelected = (item,boxId='') => {
	if(!boxId){
		boxinfo.value = item
		boxinfoRef.value.open();
	}else{
		formData.value.boxId = boxId
		boxinfoRef.value.close()
		boxListRef.value.close()
	}
	
};

const onAdd = (type) => {
	popupTitle.value = '新增物品';
	popupRef.value.open();
};

const onEdit = (item) => {
	popupTitle.value = '编辑物品';
	formData.value = item;
	popupRef.value.open();
};
const onDel = (item) => {
	store.goodsStore.del(item.id)
	reLoad()
};


const onsave = () => {
	uni.showLoading()
	formRef.value.validate().then((res) => {
			uni.hideLoading();
			
			const goods  =  store.goodsStore.list.find(item => item.name == formData.value.name)
			
			if(!goods || formData.value.id == goods.id){
				popupRef.value.close();
				store.goodsStore.save(formData.value)
				reLoad()
			}else{
				uni.$xwl.toast('物品名称已存在')
			}

	}).catch((err) => {
			uni.hideLoading();
			console.log('表单错误信息：', err);
	});
	
	
};

onLoad(() => {reLoad()});
</script>

<style lang="scss">
	.button {
		width: 60rpx;
		height: 20rpx;
	}
	.model-shell {
		width: 300rpx;
		border-radius: 6rpx;
		.form-label {
			text-align: right;
			width: 50rpx;
		}
	}
	
	.boxItem{
		width: 71.3rpx;
		height: 46rpx;
		padding: 4rpx;
	}
</style>
